/*
 * Usuario.java
 *
 * Created on 7 de Maio de 2007, 06:29
 *
 */

package net.sf.jOpinionPoll.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Entity class Usuario
 *
 * @author erico
 */
@Entity
@Table(name = "usuario")
@NamedQueries({
		@NamedQuery(name = "Usuario.fetch.findById", query = "SELECT u FROM Usuario u JOIN FETCH u.papeis r WHERE LOWER(u.id) = :id"),
        @NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u"),
        @NamedQuery(name = "Usuario.count", query = "SELECT COUNT(u) FROM Usuario u")
         })
public class Usuario implements Serializable {
	private static final long serialVersionUID = -8328559211358312181L;
	
	private String id;
    private String senha;
    private String nome;
    private String sobrenome;
    private String email;
    private String cpfcnpj;
    private TipoDeSexo sexo;
    private boolean habilitado = true;
    private Set<Papel> papeis = new HashSet<Papel>();
    
    public static enum TipoDeSexo {M, F};
    
    /** Creates a new instance of Usuario */
    public Usuario() {
    }
    
    public Usuario(String usuario) {
        this.id = usuario;
    }
    
    /**
     * Creates a new instance of Usuario with the specified values.
     * @param id do usuario
     * @senha senha do usuario
     * @param nome the nome of the Usuario
     * @param sobrenome the sobrenome of the Usuario
     * @param email the email of the Usuario
     * @param cpfcnpj the cpfcnpj of the Usuario
     * @param sexo the sexo of the Usuario
     */
    public Usuario(String id, String senha, String nome, String sobrenome,
            String email, String cpfcnpj, TipoDeSexo sexo) {
        setId(id);
        setSenha(senha);
        setNome(nome);
        setSobrenome(sobrenome);
        setCpfcnpj(cpfcnpj);
        setSexo(sexo);
    }
    
    @Id
    @Column(name = "id", nullable = false, unique = true)
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id.trim();
    }
    
    @Column(name = "senha", nullable = false)
    public String getSenha() {
        return senha;
    }
    
    public void setSenha(String senha) {
        this.senha = senha;
    }
    
    /**
     * Gets the nome of this Usuario.
     * @return the nome
     */
    @Column(name = "nome", nullable = false)
    public String getNome() {
        return this.nome;
    }
    
    /**
     * Sets the nome of this Usuario to the specified value.
     * @param nome the new nome
     */
    public void setNome(String nome) {
        this.nome = nome;
    }
    
    /**
     * Gets the sobrenome of this Usuario.
     * @return the sobrenome
     */
    @Column(name = "sobrenome", nullable = false)
    public String getSobrenome() {
        return this.sobrenome;
    }
    
    /**
     * Sets the sobrenome of this Usuario to the specified value.
     * @param sobrenome the new sobrenome
     */
    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }
    
    /**
     * Gets the email of this Usuario.
     * @return the email
     */
    @Column(name = "email", nullable = false)
    public String getEmail() {
        return this.email;
    }
    
    /**
     * Sets the email of this Usuario to the specified value.
     * @param email the new email
     */
    public void setEmail(String email) {
        this.email = email;
    }
    
    /**
     * Gets the cpfcnpj of this Usuario.
     * @return the cpfcnpj
     */
    @Column(name = "cpfcnpj", nullable = false)
    public String getCpfcnpj() {
        return this.cpfcnpj;
    }
    
    /**
     * Sets the cpfcnpj of this Usuario to the specified value.
     * @param cpfcnpj the new cpfcnpj
     */
    public void setCpfcnpj(String cpfcnpj) {
        this.cpfcnpj = cpfcnpj;
    }
    
    /**
     * Gets the sexo of this Usuario.
     * @return the sexo
     */
    @Column(name = "sexo", nullable = false)
    @Enumerated(EnumType.STRING)
    public TipoDeSexo getSexo() {
        return this.sexo;
    }
    
    /**
     * Sets the sexo of this Usuario to the specified value.
     * @param sexo the new sexo
     */
    public void setSexo(TipoDeSexo sexo) {
        this.sexo = sexo;
    }
    
    @Column(name = "habilitado", nullable = false)
    public boolean getHabilitado() {
    	return habilitado;
    }

    public void setHabilitado(boolean e) {
    	habilitado = e;
    }
    
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name="usuario_papel",
		joinColumns=@JoinColumn(name="usuario_id",
			referencedColumnName="id"),
		inverseJoinColumns=@JoinColumn(name="papel_nome",
			referencedColumnName="nome"))
    public Set<Papel> getPapeis() {
    	return papeis;
    }
    
    public void setPapeis(Set<Papel> p) {
    	papeis = p;
    }
    
    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        return (getId() != null ? getId().hashCode() : 0);
    }
    
    /**
     * Determines whether another object is equal to this Usuario.  The result is
     * <code>true</code> if and only if the argument is not null and is a Usuario object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object o) {
    	if (o == null || !(o instanceof Usuario))
            return false;

        if (getNome() != null)
	        return (getNome().equals(((Usuario)o).getNome()));
        
        return this == o;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return this.getClass().getName() + "[pk=" + getId() + "]";
    }
}